package cn.cyh.pet.controller;


import cn.cyh.basic.uitls.AJaxResult;
import cn.cyh.basic.uitls.LoginContext;
import cn.cyh.basic.uitls.PageList;
import cn.cyh.pet.domain.Pet;
import cn.cyh.pet.query.PetQuery;
import cn.cyh.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    //  分页+高级查询
    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery query, HttpServletRequest request){
        return petService.queryPage(query);
    }
    @PostMapping("/onsale")
    public AJaxResult onSale(@RequestBody List<Long> ids){
        try {

            petService.onSale(ids);
            return AJaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AJaxResult.myself().setMsg("操作失败");
        }
    }
    @PostMapping("/offsale")
    public AJaxResult offSale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return AJaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AJaxResult.myself().setMsg("操作失败");
        }
    }
    @GetMapping("/{id}")
    public Pet queryById(@PathVariable("id") Long id){
        return petService.loadById(id);
    }

    @GetMapping("/adopt/{id}")
    public AJaxResult adoptPet(@PathVariable("id") Long id, HttpServletRequest request){

        try {
            petService.adoptPet(id, LoginContext.currentLongInfo(request).getId());
            return AJaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AJaxResult.myself().setMsg("系统繁忙");
        }
    }

}
